<?php

namespace App\Http\Controllers;

use App\Facades\AuthService;
use Illuminate\Http\Request;

/**
 * LoginController
 *
 * @package App\Http\Controllers
 * @license [http://www.85do.com] [杭州永奥网络科技有限公司]
 * @copyright Copyright (c) 2018-2026 Hangzhou Yongao Technology Co., Ltd. All rights reserved.
 */
class LoginController
{
    protected $api;

    /**
     * Create a new controller instance.
     *
     * @return LoginController
     */
    public function __construct()
    {
        $this->api = app('cmp')->login;
    }

    /**
     * 首页
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $test = 'test';
        return view('login',compact('test'));
    }

    /**
     * 登录
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function login(Request $request)
    {
        $data = [
          'username'=> $request->get('UserName'),
          'password'=> $request->get('Password')
        ];

        try{
            $user = $this->api->login($data);
        } catch (\Exception $e){
            return redirect('/login')->with('message', $e->getMessage())->withInput(
                $request->flashOnly(['UserName'])
            );
        }

        AuthService::login($user->item);

        return redirect('/mc');
    }
}